package framework.mashup;

import java.net.URL;

import netscape.javascript.JSObject;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.layout.Pane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.concurrent.Worker.State;

public class GoogleMap extends Pane {
	private WebEngine webEngine;
	private GoogleMapListener listener;
	private Boolean controlVisible;
	
	public GoogleMap(boolean controlVisible) {
		this.controlVisible = controlVisible;
		WebView webView = new WebView();
		webEngine = webView.getEngine();
		URL url = getClass().getResource("StaticHtml.html");
        getChildren().add(webView);
        webView.prefHeightProperty().bind(heightProperty());
        webView.prefWidthProperty().bind(widthProperty());
        
        webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
			@Override
			public void changed(ObservableValue<? extends State> arg0,
					State prevState, State newState) {

				if (newState.equals(State.SUCCEEDED)) {
					initialize();
					if (listener != null) {
						listener.mapReady();
					}
				}
			}
        });
        webEngine.load(url.toExternalForm());
	}
	
	public void setStateListener(GoogleMapListener listener) {
		this.listener = listener;
	}
	
	public void drawPolyline(LatLng[] nodes) {
		for (int i = 1; i < nodes.length; i++) {
			webEngine.executeScript("document.drawLine2(" + nodes[i-1].lat.toString() + "," + nodes[i-1].lng.toString() + "," + 
						nodes[i].lat.toString() + "," + nodes[i].lng.toString() + ")");
		}
	}
	
	public void goToLocaltion(final String location) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				webEngine.executeScript("document.goToLocation('" + location + "')");
			}
		});
	}
	
	public void addMarker(final Double lat, final Double lng, final String imageFile) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				try {
					webEngine.executeScript("document.addMarker(" + lat.toString() + "," + lng.toString() + "," + 
							"\"" + imageFile + "\"" + ")");
				
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public void moveMarker(final Double lat, final Double lng, final Integer index) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				webEngine.executeScript("document.moveMarker(" + lat.toString() + "," + lng.toString() + "," + index.toString() + ")");
				getAddress(index);
			}
		});
	}

	public void goToLocaltion(final Double lat, final Double lng) {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				webEngine.executeScript("document.goToLocationLatLng(" + lat.toString() + "," + lng.toString() + ")");
			}
		});
	}

	public void removeMarker(Integer index) {
		webEngine.executeScript("document.removeMarker(" + index.toString() + ")");
	}

	public Double getDistance(Double lat, Double lng, Double lat2, Double lng2) {
		return (Double)webEngine.executeScript("document.getDistance(" + lat.toString() + "," + lng.toString() + "," + 
				lat2.toString() + "," + lng2.toString() + ")");
	}

	public void addMarker(LatLng latLng, String imageFile) {
		addMarker(latLng.lat, latLng.lng, imageFile);
	}

	public void clearMarker() {
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				try {
					webEngine.executeScript("document.clearMarker()");
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			}
			
		});
		
	}

	private void initialize() {
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
			//	webEngine.executeScript("document.initMap(" + controlVisible.toString() + ")");
				Object ret = webEngine.executeScript("window");
				if (ret instanceof JSObject) {
					((JSObject)ret).setMember("app", new JavaApp(listener));
				}
				else if (ret instanceof String) {
				//	System.out.println(ret);
				}
				else {
				//	System.out.println();
				}
			}
		});
		
	}

	
	public String getAddress(final Integer index) {
		try {
			webEngine.executeScript("document.getAddress(" + index.toString() + ")");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		return "";
	}

}
